package com.project.nimsen.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.SiteDto;
import com.project.nimsen.entity.Site;
import com.project.nimsen.service.ISiteService;
import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
@Repository("siteService")
public class SiteServiceImpl extends BaseServiceImpl implements ISiteService {
	@Resource
	IBaseDao baseDao;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public List<Map<String, Object>> find(SiteDto dto) throws Exception {
		Finder finder = new Finder("select si.id,si.sitename,si.sitetag,si.managerid,"
			+ "si.createUser,si.createTime,si.active, mu.username as managername, cu.username as "
			+ "createname,si.domainname,si.indexmodel,si.phoneindexmodel,si.padindexmodel,"
			+ "si.modelid,si.phonemodelid,si.padmodelid from site as si left join user as"
			+ " cu on si.createUser=cu.id left join user as mu on si.managerid=mu.id where "
			+ "si.active!=1 and cu.id!=1 and mu.id!=1");
		if(StringUtils.isNotBlank(dto.getValifield())){
			finder.append(" and si.").append(dto.getValifield()).append("=:valivalue");
			finder.setParam("valivalue", dto.getValivalue());
		}
		return baseDao.queryForList(finder, dto.getPage());
	}
	public boolean deleteByManagerId(String managerId) throws Exception {
		if(StringUtils.isBlank(managerId))
			return false;
		Finder finder = new Finder("update site set active=1 where active!=1 and managerid=:managerid");
		finder.setParam("managerid", managerId);
		return baseDao.update(finder)>0;
	}
	public Map<String, Object> findByDto(SiteDto dto) throws Exception {
		Finder finder = new Finder("select id,managerid,sitename,sitetag,createUser,createTime,active,"
			+ "domainname,indexmodel,phoneindexmodel,padindexmodel,modelid,phonemodelid,padmodelid "
			+ "from site where active!=1");
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and id=:id");
			finder.setParam("id", dto.getId());
		}
		if(StringUtils.isNotBlank(dto.getManagerid())){
			finder.append(" and managerid=:managerid");
			finder.setParam("managerid", dto.getManagerid());
		}
		if(StringUtils.isNotBlank(dto.getDomainname())){
			finder.append(" and domainname=:domainname");
			finder.setParam("domainname", dto.getDomainname());
		}
		return baseDao.queryForObject(finder);
	}
	public Site findEntityById(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return null;
		Finder finder = new Finder("select id,managerid,sitename,sitetag,createUser,createTime,active"
			+ ",domainname,indexmodel,phoneindexmodel,padindexmodel,modelid,phonemodelid,padmodelid "
			+ "from site where active!=1 and id=:id");
		finder.setParam("id", id);
		return baseDao.queryForObject(finder, Site.class);
	}
	public Map<String, Object> findVilidate(SiteDto dto) throws Exception {
		if(dto==null)
			return null;
		Finder finder = new Finder("select id from site where active!=1");
		finder.append(" and ").append(dto.getValifield()).append("=:filed");
		finder.setParam("filed", dto.getValivalue());
		//如果id存在表示为修改
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and id!=:id");
			finder.setParam("id", dto.getId());
		}
		return baseDao.queryForObject(finder);
	}
	public Map<String, Object> findByManagerId(String managerid)
			throws Exception {
		SiteDto dto = new SiteDto();
		dto.setManagerid(managerid);
		return findByDto(dto);
	}
	public Site findEntityByUserid(String userid) throws Exception {
		if(StringUtils.isBlank(userid))
			return null;
		Finder finder = new Finder("select id,managerid,sitename,sitetag,createUser,createTime,active"
			+ ",domainname,indexmodel,phoneindexmodel,padindexmodel,modelid,phonemodelid,padmodelid "
			+ "from site where active!=1 and managerid=:id");
		finder.setParam("id", userid);
		return baseDao.queryForObject(finder, Site.class);
	}
	public Site findEntityByDomainame(String domainame) throws Exception {
		if(StringUtils.isBlank(domainame))
			return null;
		Finder finder = new Finder("select id,managerid,sitename,sitetag,createUser,createTime,active"
			+ ",domainname,indexmodel,phoneindexmodel,padindexmodel,modelid,phonemodelid,padmodelid "
			+ "from site where active!=1 and domainname=:domainame");
		finder.setParam("domainame", domainame);
		return baseDao.queryForObject(finder, Site.class);
	}
}
